package com.cooba.content_frament;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.activitys.BookTicketsActivity;
import com.cooba.application.MyApplication;
import com.cooba.bean.BannerBean;
import com.cooba.utils.ACache;
import com.cooba.utils.BannerUtils;
import com.cooba.utils.CarURL;
import com.cooba.utils.Constants;
import com.cooba.utils.ServerURL;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2015/12/7.
 * find
 */
public class FindFrament extends Fragment implements View.OnClickListener {

    private LinearLayout banner_ll;
    private LinearLayout food;
    private LinearLayout film;
    private LinearLayout hotel;
    private LinearLayout KTV;
    private LinearLayout rental;
    private LinearLayout tourism;
    private LinearLayout bt_book;
    private LinearLayout let;
    private LinearLayout rent_car;
    private LinearLayout special_car;
    private HttpUtils httpUtils;
    private RequestParams params;
    private BannerBean banner;
    private boolean isIncar;
    private String mac;
    private ACache acache;
    private PopupWindow mTicketPop;
    private Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    private View contentview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (contentview == null) {
            contentview = initview(inflater);
        }
        return contentview;
    }

    private void initPopUpWindow() {
        View chooseView =getActivity().getLayoutInflater().inflate(R.layout.popup_ticket_dialog, null);
        mTicketPop = new PopupWindow(chooseView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mTicketPop.setFocusable(true);
        mTicketPop.setOutsideTouchable(true);
        mTicketPop.setBackgroundDrawable(new ColorDrawable());
        chooseView.findViewById(R.id.tv_train).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mTicketPop != null) {
                    mTicketPop.dismiss();
                }
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.汽车票);
                startActivity(intent);
            }
        });

        chooseView.findViewById(R.id.tv_air).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mTicketPop != null) {
                    mTicketPop.dismiss();
                }
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.飞机票);
                startActivity(intent);
            }
        });

    }
    private void showTicketChoosePop(View view) {
        if (mTicketPop == null) {
            return;
        }
        if (mTicketPop.isShowing()) {
            mTicketPop.dismiss();
        } else {
           // mTicketPop.showAsDropDown(view);
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            mTicketPop.getContentView().measure(0, 0);
             mTicketPop.showAtLocation(view, Gravity.NO_GRAVITY, location[0]-mTicketPop.getContentView().getMeasuredWidth()/4, location[1]-mTicketPop.getContentView().getMeasuredHeight());

        }
    }


    //初始化控件
    private View initview(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.find_frament, null, false);
        banner_ll= (LinearLayout) view.findViewById(R.id.find_banner_ll);
        food = (LinearLayout) view.findViewById(R.id.food);
        film = (LinearLayout) view.findViewById(R.id.film);
        KTV = (LinearLayout) view.findViewById(R.id.KTV);
        hotel = (LinearLayout) view.findViewById(R.id.hotel);
        rental = (LinearLayout) view.findViewById(R.id.rental);
        tourism = (LinearLayout) view.findViewById(R.id.tourism);
        bt_book = (LinearLayout) view.findViewById(R.id.bt_book);
        let = (LinearLayout) view.findViewById(R.id.let);
        rent_car= (LinearLayout) view.findViewById(R.id.rent_car_ll);
        special_car= (LinearLayout) view.findViewById(R.id.speical_car_ll);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = (int) (width * 0.56);
        //banner_ll.setBackground(getResources().getDrawable(R.mipmap.default_icon));
        ImageView image=new ImageView(getContext());
        image.setLayoutParams(new LinearLayout.LayoutParams(width, height));
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        image.setImageResource(R.mipmap.default_icon);
        banner_ll.addView(image);
        initPopUpWindow();
        initIncar();
        init();
        acache = ACache.get(getContext());

        getBanner();
        return view;
    }

    //获取顶部广告条
    private void getBanner() {
        String urlbanner;
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String now_time = sDateFormat.format(new java.util.Date());
        httpUtils = new HttpUtils();
        params = new RequestParams();
        params.addBodyParameter("now_time", now_time);
        params.addBodyParameter("tag", "server");
        if (isIncar) {
            params.addBodyParameter("mac", mac);
            urlbanner = CarURL.bannerList;
        } else {
            urlbanner = ServerURL.httpUrl + ServerURL.bannerList;
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, urlbanner, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("wei", "获取失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("wei", "获取成功" + responseInfo.result);
                try {
                    banner = new Gson().fromJson(responseInfo.result, BannerBean.class);
                    if (banner.getBannerList() != null && banner.getBannerList().size() > 0) {
                        initHeadView();
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initHeadView() {
        banner_ll.removeAllViews();
        banner_ll.addView(BannerUtils.getCustomHeaderView(getContext(),banner.getBannerList()));
    }

    private void init() {
        food.setOnClickListener(this);
        film.setOnClickListener(this);
        KTV.setOnClickListener(this);
        rental.setOnClickListener(this);
        tourism.setOnClickListener(this);
        bt_book.setOnClickListener(this);
        let.setOnClickListener(this);
        hotel.setOnClickListener(this);
        rent_car.setOnClickListener(this);
        special_car.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.food:
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.美食);
                startActivity(intent);
                break;
            case R.id.film:
//                intent = new Intent(getActivity(), BookTicketsActivity.class);
//                intent.putExtra("mMrl", ServerURL.美食);
//                startActivity(intent);
                Toast.makeText(getActivity(),"暂未开通服务!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.hotel:
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.酒店);
                startActivity(intent);
                break;
            case R.id.KTV:
//                intent = new Intent(getActivity(), BookTicketsActivity.class);
//                intent.putExtra("mMrl", ServerURL.美食);
//                startActivity(intent);
                Toast.makeText(getActivity(),"暂未开通服务!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rental:
//                intent = new Intent(getActivity(), BookTicketsActivity.class);
//                intent.putExtra("mMrl", ServerURL.美食);
//                startActivity(intent);
                Toast.makeText(getActivity(),"暂未开通服务!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tourism:
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.旅游);
                startActivity(intent);
                break;
            case R.id.bt_book:
//                intent = new Intent(getActivity(), BookTicketsActivity.class);
//                intent.putExtra("mMrl", ServerURL.汽车票);
//                startActivity(intent);
                showTicketChoosePop(v);
                break;
            case R.id.let:
//                intent = new Intent(getActivity(), BookTicketsActivity.class);
//                intent.putExtra("mMrl", ServerURL.美食);
//                startActivity(intent);
                Toast.makeText(getActivity(),"暂未开通服务!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rent_car_ll:
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.国内租车);
                startActivity(intent);
                break;
            case R.id.speical_car_ll:
                intent = new Intent(getActivity(), BookTicketsActivity.class);
                intent.putExtra("mMrl", ServerURL.打车服务);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("wei--", "Find:" + isVisibleToUser);
//        initIncar();
//        if (getUserVisibleHint()) {
//            if (banner == null)
//                getBanner();
//        }
    }

    public void initIncar() {
        MyApplication application = MyApplication.getMyApplication();
        isIncar = application.getIsInCar();
        mac = application.getMac();
    }
}
